﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Barebones.Components;
using Microsoft.Xna.Framework.Graphics;
using Barebones.Dependencies;
using Meat.Resources;
using Microsoft.Xna.Framework;

namespace Meat.Rendering
{
    public class UserVertexModifier : EntityComponent
    {
        Vector2 m_BillboardSize;
        Handle<UserVertexResource<VertexPositionNormalTexture>> m_UserVerticeHandle;

        public float MouvementModifier
        { get; set; }

        public override IEnumerable<Barebones.Dependencies.IDependency> GetDependencies()
        {
            yield return new Dependency<Handle<UserVertexResource<VertexPositionNormalTexture>>>(item => m_UserVerticeHandle = item);
        }

        public UserVertexModifier(Vector2 billboardsize)
        {
            MouvementModifier = 1.0f;
            m_BillboardSize = billboardsize;
        }

        public void TranslateTexCoordsByWorld(Vector2 world)
        {
            Vector2 ratio = world / m_BillboardSize;
            ratio *= MouvementModifier;

            VertexPositionNormalTexture[] buffer = m_UserVerticeHandle.Get().Buffer;
            for (int i = 0; i < buffer.Length; ++i)
            {
                buffer[i].TextureCoordinate += ratio;
            }
        }

        public void TranslateTexCoords(Vector2 amount)
        {
            amount *= MouvementModifier;

            VertexPositionNormalTexture[] buffer = m_UserVerticeHandle.Get().Buffer;
            for(int i = 0; i < buffer.Length; ++i)
            {
                buffer[i].TextureCoordinate += amount;
            }
        }
    }
}
